﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ruici.Web.Services.CRS_Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Ruici.Web.Services
{
    public class UserReservitionService
    {
        private CRS_ServiceSoapClient crsService = new CRS_ServiceSoapClient();

        private JsonParser jsonParser = new JsonParser();
        /// <summary>
        /// 获取用户预约列表
        /// </summary>
        /// <param name="ipid"></param>
        /// <returns></returns>
        public List<resvIntention> ResvIntention_GetList(int iPid)
        {
            ReturnModelOfListOfresvIntention ListOfResv =  crsService.ResvIntention_GetList(iPid);
            List<resvIntention> resvIntentionlist = new List<resvIntention>();

            foreach(resvIntention resvInt in ListOfResv.Data)
            {
                resvIntentionlist.Add(resvInt);
            }
            
            return resvIntentionlist;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="type"></param>
        /// <param name="hospId"></param>
        /// <param name="remoteHospId"></param>
        /// <param name="depId"></param>
        /// <param name="depTypeId"></param>
        /// <param name="doctorId"></param>
        /// <param name="timePeriod"></param>
        /// <param name="resvDate"></param>
        /// <param name="description"></param>
        /// <param name="pictures"></param>
        /// <param name="userIP"></param>
        /// <param name="platform"></param>
        /// <param name="OpenId"></param>
        /// <param name="userInfoJson"></param>
        /// <param name="operatorPid"></param>
        public string ResvIntention_Add(int pid,
                    int type,
                    int hospId,
                    int remoteHospId,
                    int depId,
                    int depTypeId,
                    int doctorId,
                    string timePeriod,
                    DateTime resvDate,
                    string description,
                    string pictures,
                    string userIP,
                    string platform,
                    string OpenId,
                    string userInfoJson,
                    int operatorPid)
        {
            return crsService.ResvIntention_Add(pid, type, hospId, remoteHospId, depId, depTypeId, doctorId, timePeriod, resvDate, description, pictures, userIP, platform, OpenId, userInfoJson, operatorPid).Data;
        }

        /// <summary>
        /// 添加来访病人信息
        /// </summary>
        /// <param name="iPID"></param>
        /// <param name="editName">编辑姓名</param>
        /// <param name="editPhone">编辑手机号</param>
        /// <param name="editCard">卡号</param>
        /// <param name="isDefault">是否默认</param>
        /// <param name="sGender">性别</param>
        /// <param name="birthDate">生日</param>
        /// <returns></returns>
        public ReturnModelOfInt32 AddPatientVisitor(int iPID, string editName, string editPhone, string editCard, bool isDefault, string sGender, DateTime birthDate)
        {
            return crsService.FamilyMember_Add(iPID,editName,editPhone,editCard,isDefault,string.Empty,null,sGender,string.Empty,birthDate);
        }
        /// <summary>
        /// 获取案例列表
        /// </summary>
        /// <param name="iPid"></param>
        /// <param name="OrgId"></param>
        /// <param name="visitID"></param>
        /// <returns></returns>
        public string GetUserCaseListByTrID(int iPid, int OrgId, int visitID)
        {
            return crsService.EMR_UserCase_GetByTrID(visitID, OrgId, iPid).Data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPID"></param>
        /// <param name="editName"></param>
        /// <param name="Phone"></param>
        /// <param name="IdCardNo"></param>
        /// <param name="isDefault"></param>
        /// <param name="sGender"></param>
        /// <param name="birthDate"></param>
        /// <returns></returns>
        public ReturnModelOfFamilyMember UpatePatientVisitor(int visitID, string editName, string Phone, string IdCardNo, bool isDefault, string sGender, DateTime birthDate)
        {
            return crsService.FamilyMember_Update(visitID, string.Empty,editName, Phone, string.Empty,birthDate,sGender, IdCardNo, isDefault);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="visitID"></param>
        /// <returns></returns>
        public bool DeletePatientVisitor(int visitID)
        {
            return crsService.FamilyMember_Delete(visitID).Success;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPid"></param>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public string GetEReportDetail(int caseId, int orgId)
        {
            return crsService.EMR_UserCase_GetHTML(caseId, orgId).Data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reportId"></param>
        /// <param name="orgid"></param>
        /// <param name="reportType"></param>
        /// <returns></returns>
        public string GetReportDetail(int reportId, int orgid, string reportType)
        {
            return crsService.EMR_ReportDetail_Get(reportId, orgid, reportType).Data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reportId"></param>
        /// <param name="caseId"></param>
        /// <param name="reportType"></param>
        /// <returns></returns>
        public string getCaseReportDetail(int reportId, int caseId, string reportType)
        {
            return crsService.EMR_ReportDetail_GetHTML(reportId, caseId, reportType).Data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPid"></param>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public string GetEReportList(int caseId)
        {
            return crsService.EMR_Report_Get(caseId).Data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iPid"></param>
        /// <returns></returns>
        public List<FamilyMember> GetPationtVisitorList(int iPid)
        {
            ReturnModelOfListOfFamilyMember familyMemeberListReturnModel =  crsService.FamilyMember_GetList(iPid);
            List<FamilyMember> familyMemeberList = new List<FamilyMember>();

            foreach(FamilyMember member in familyMemeberListReturnModel.Data)
            {
                familyMemeberList.Add(member);
            }

            return familyMemeberList;
        }

        /// <summary>
        /// 通过PID获取当前登录用户的病例信息
        /// </summary>
        /// <param name="iPID"></param>
        /// <returns></returns>
        public string GetPatientInfoList(int iPID)
        {
            var patientInfor = crsService.EMR_UserCase_Get(iPID);
            return patientInfor.Data; 
        }

        public JEnumerable<JToken> GetDoctorListByNumber(string DocIdList)
        {
            JArray patientInfoArray = null;
            JEnumerable<JToken> patinetInforList;
            ReturnModel docList =  crsService.getDoctorListByNumber(DocIdList);
            if(docList.Data != null)
            {
                patientInfoArray = (JArray)JsonConvert.DeserializeObject(docList.Data.ToString());
                patinetInforList = patientInfoArray.Children();
            }
            return patinetInforList;
        }
    }
}
